﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;
using MyTestMaterial = DDDTest.Helper.MyTestMaterial;

namespace DDDTest.Core {
    using Object = DDD.Core.Object;

    [TestClass]
    public class TestAnimationPlayerAnimate {

        /// <summary>
        /// 標準的なアニメーションのテスト
        /// </summary>
        /// <remarks>
        /// ２つのクリップが同一のオブジェクト・プロパティを変化させる
        /// </remarks>
        [TestMethod]
        public void Test_Animate1 () {
            var player = new AnimationPlayer ();
            var obj = new MyTestMaterial ();

            var clip1 = new AnimationClip ("Clip 1");

            var track1 = new AnimationTrack (2, 1, InterpolationType.Linear);
            track1.SetKeyframe (0, 0, new float[] { 0 });
            track1.SetKeyframe (1, 100, new float[] { 1 });
            track1.SetDuration (100);

            clip1.AddTracker (obj, DDD.Core.Object.GetName (() => obj.R32f), track1);
            player.AddClip (clip1);

            var clip2 = new AnimationClip ("Clip 2");

            var track2 = new AnimationTrack (2, 1, InterpolationType.Linear);
            track2.SetKeyframe (0, 0, new float[] { 1 });
            track2.SetKeyframe (1, 100, new float[] { 2 });
            track2.SetDuration (100);

            clip2.AddTracker (obj, DDD.Core.Object.GetName (() => obj.R32f), track2);
            player.AddClip (clip2);

            // 標準
            clip1.SetWeight (1);
            clip2.SetWeight (1);
            player.Animate (50);
            Assert.AreEqual (2.0f, obj.R32f, 0.0001f);     // 0.5 + 1.5

            // ウェイトの変化(0:2)
            clip1.SetWeight (0);
            clip2.SetWeight (2);
            player.Animate (50);
            Assert.AreEqual (3.0f, obj.R32f, 0.0001f);     // 0.5*0 + 1.5*2

            // スピードの変化
            clip1.SetWeight (1);
            clip2.SetWeight (1);
            clip1.SetSpeed (2.0f, 0);
            clip2.SetSpeed (2.0f, 0);
            player.Animate (50);
            Assert.AreEqual (3.0f, obj.R32f, 0.0001f);     // 1 + 2

            // 再生位置の変化
            clip1.SetPlaybackPosition (0, 50);
            clip2.SetPlaybackPosition (0, 50);
            player.Animate (50);
            Assert.AreEqual (1.0f, obj.R32f, 0.0001f);     // 0 + 1
            player.Animate (100);
            Assert.AreEqual (3.0f, obj.R32f, 0.0001f);     // 1 + 2
        }

    }
}
